<?php
/**
 +------------------------------------------------------------------------------
 * CountAction控制器类
 +------------------------------------------------------------------------------
 * @category   自动统计
 * @package  app
 * @subpackage  Action
 * @author   tb
 * @date 2014.03.26
 * @time  下午2:44:22
 +------------------------------------------------------------------------------
 */
class CountAction extends BaseAction {
	protected $dao,$changt_str;
	
	function _initialize() {
		parent::_initialize ();
		$this->changt_str=array(
				'ADMIN_ADD'=>'财务充值',
				'ORDER_USE'=>'订单消费',
				'ORDER_BAK'=>'订单退款',
				'PV_CHANG'=>'PV变动',
				'MV_CHANG'=>'MV变动',
				'TENPAY_ADD'=>'财付通',
				'ALIPAY_ADD'=>'支付宝',
				'COUPON_CHANG'=>'PV换优惠券',
				'ADMIN_BV' => '赠送BV',
				'CVTOMV' => 'CV转MV',
				'couponxfcz' => '赠送优惠券',
				'couponxf' => '优惠券'
		);
		$this->dao = M("UserAssets");
		$this->assign('changtype',$this->changt_str);
	}
	

	/**
	 * 自动统计，昨日统计
	 * @author tb
	 * @date 2014.03.26
	 * */
	function daycount(){
		$model = M("MoneyStatistics");
		$date = date("Y-m-d", strtotime("1 days ago"));	
		$time['btime'] = gettime($date." 00:00:00");
		$time['etime'] = gettime($date." 23:59:59");
		
		//判断是否执行
		$where['type'] = 1;
		$where['start_date'] = $time['btime'];
		$where['end_date'] = $time['etime'];
		$check = $model->where($where)->find();
		if(!$check){
			//余额
			$bdate = date("Y-m-d", strtotime("2 days ago"));
			$where['start_date'] = gettime($bdate." 00:00:00");
			$where['end_date'] = gettime($bdate." 23:59:59");
			$where['type'] = 1;
			$res = $model->where($where)->find();
			
			//收入
			$czlist = self::asset_report("'ADMIN_ADD','TENPAY_ADD','ALIPAY_ADD' ","'CV'",$time);
			$coupon = self::coupon_total($time);
			foreach ($czlist as $vo){
				$earning += $vo['value'];
			}
			$earning += $coupon['total'];
			
			//支出
			$xflist = self::asset_report("'ORDER_USE'","'CV'",$time);
			foreach ($xflist as $xf){
				$expend += $xf['value'];
			}
			$expend += $coupon['use'];
			//结余
			$surplus = $earning - $expend;
			
			$data['balance'] = empty($res['surplus']) ? 0 : $res['surplus'];
			$data['earning'] = $earning;
			$data['expend'] = $expend;
			$data['surplus'] = $surplus;
			$data['type'] = $where['type'] = 1;
			$data['userid'] = $this->userid;
			$data['start_date'] =$time['btime'];
			$data['end_date'] = $time['etime'];
			$data['ctime'] = gettime(date("Y-m-d H:i:s"));
			$data['remark'] = '自动统计';			
			$reid = $model->add($data);	
			
		}
	}	
	
	
	/**
	 * 自动统计，上周统计
	 * @author tb
	 * @date 2014.03.26
	 * */
	function weekcount(){
		$model = M("MoneyStatistics");
		$weekend =  date("Y-m-d",strtotime("-1 week Sunday"));
		$weekbegin = date("Y-m-d", strtotime("$weekend - 6 days"));
		$time['btime'] = gettime($weekbegin." 00:00:00");
		$time['etime'] = gettime($weekend." 23:59:59");
		
		//判断是否执行
		$where['type'] = 2;
		$where['start_date'] = $time['btime'];
		$where['end_date'] = $time['etime'];
		$check = $model->where($where)->find();
		
		if(!$check){
			//余额
			$b_weekend =  date("Y-m-d",strtotime("-2 week Sunday"));
			$b_weekbegin = date("Y-m-d", strtotime("$b_weekend - 6 days"));
			$where['start_date'] = gettime($b_weekend." 00:00:00");
			$where['end_date'] = gettime($b_weekbegin." 23:59:59");
			$where['type'] = 2;
			$res = $model->where($where)->find();
		
			//收入
			$czlist = self::asset_report("'ADMIN_ADD','TENPAY_ADD','ALIPAY_ADD' ","'CV'",$time);
			$coupon = self::coupon_total($time);
			foreach ($czlist as $vo){
				$earning += $vo['value'];
			}
			$earning += $coupon['total'];
	
			//支出
			$xflist = self::asset_report("'ORDER_USE'","'CV'",$time);
			foreach ($xflist as $xf){
				$expend += $xf['value'];
			}
			$expend += $coupon['use'];
			//结余
			$surplus = $earning - $expend;
		
			$data['balance'] = empty($res['surplus']) ? 0 : $res['surplus'];
			$data['earning'] = $earning;
			$data['expend'] = $expend;
			$data['surplus'] = $surplus;
			$data['type'] =  2;
			$data['userid'] = $this->userid;
			$data['start_date']  = $time['btime'];
			$data['end_date'] = $time['etime'];
			$data['ctime'] = gettime(date("Y-m-d H:i:s"));
			$data['remark'] = '自动统计';
			$check = $model->where($where)->find();
			if(!$check){
				$reid = $model->add($data);
			}
		}
	}
	
	
	/**
	 * 自动统计，上月统计
	 * @author tb
	 * @date 2014.03.26
	 * */
	function monthcount(){
		$model = M("MoneyStatistics");
		
	    $time['btime'] = gettime(date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))));
		$time['etime'] = gettime(date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))));
		//判断是否执行
		$where['type'] = 3;
		$where['start_date'] = $time['btime'];
		$where['end_date'] = $time['etime'];
		$check = $model->where($where)->find();
		
		if(!$check){
			//余额
			$b_monthbegion = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-2,1,date("Y")));
			$b_monthend = date("Y-m-d H:i:s",mktime(23,59,59,date("m")-1 ,0,date("Y")));
	
			$where['start_date'] = gettime($b_monthbegion);
			$where['end_date'] = gettime($b_monthend);
			$where['type'] = 3;
			$res = $model->where($where)->find();
		
			//收入
			$czlist = self::asset_report("'ADMIN_ADD','TENPAY_ADD','ALIPAY_ADD' ","'CV'",$time);
			$coupon = self::coupon_total($time);
			foreach ($czlist as $vo){
				$earning += $vo['value'];
			}
			$earning += $coupon['total'];
		
			//支出
			$xflist = self::asset_report("'ORDER_USE'","'CV'",$time);
			foreach ($xflist as $xf){
				$expend += $xf['value'];
			}
			$expend += $coupon['use'];
			//结余
			$surplus = $earning - $expend;
		
			$data['balance'] = empty($res['surplus']) ? 0 : $res['surplus'];
			$data['earning'] = $earning;
			$data['expend'] = $expend;
			$data['surplus'] = $surplus;
			$data['type'] = $where['type'] = 3;
			$data['userid'] = $this->userid;
			$data['start_date'] = $where['start_date'] = $time['btime'];
			$data['end_date'] = $where['end_date'] = $time['etime'];
			$data['ctime'] = gettime(date("Y-m-d H:i:s"));
			$data['remark'] = '自动统计';
			$check = $model->where($where)->find();
			if(!$check){
				$reid = $model->add($data);
			}
		}
	}
	
	
	
	/**
	 * 消费充值报表
	 * @author tb
	 * @date 2014.03.18
	 * */
	function asset_report($changtype,$subject,$time){		
		$where = '';
		if($subject){
			$where .= " and `subject` in ($subject) ";
		}
		if($time){
			$starttime = $time['btime'];
			$endtime = $time['etime'];
		}
		if($starttime && $endtime){
			$where .= " and ctime between $starttime and $endtime ";
		}elseif($starttime){
			$where .=" and (ctime > $starttime or ctime = $starttime) ";
		}elseif($endtime){
			$where .= " and (ctime < $endtime or ctime = $endtime) ";
		}
		$sql = "select changtype,`subject`,round(sum(`value`)/100,2) as value from user_assets where platform = 'SMW01' and changtype in (".$changtype.")".$where."  GROUP BY changtype,`subject`";
		$res = $this->dao->query($sql);
		$this->assign('con',$conditon);
		return $res;
	}
	
	/**
	 * 用户优惠券统计
	 * @author tb
	 * @date 2014.03.18
	 * */
	function coupon_total($time){
		$dao = M("UserCoupon");
		$where = array();
		$starttime = $time['btime'];
		$endtime = $time['etime'];
		$where = parent::create_date(array('stime'=>$starttime,'etime'=>$endtime),'createtime',$where);
		$coupon['total'] = round($dao->where($where)->sum("value/100"),2);
		$where['status'] = 1;
		$coupon['use'] = round($dao->where($where)->sum("value/100"),2);
		$where['status'] = 0;
		$coupon['nouse'] = round($dao->where($where)->sum("value/100"),2);
		return $coupon; 
	}
	
	
}//end AssetsreportAction